package lzupdater.gui;

import java.io.StringReader;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.html.HTMLEditorKit;

import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.locale.Dictionary;
import lzupdater.locale.Locale;
import lzupdater.locale.LocaleListener;
import lzupdater.locale.LocalizedString;
import lzupdater.log.Logger;
import lzupdater.log.LoggerLevel;
import lzupdater.net.HTTPData;
import lzupdater.utils.LZULogger;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

public class DebugPanel extends HubComponent implements LocaleListener {

	private final static LocalizedString debugTitle = new LocalizedString("Debug");
	private final static LocalizedString pageTitle = new LocalizedString("page %d");
	
	private Logger logger;
	private JFrame frame;
	private JTabbedPane jTab;
	private MainGui mainGui;

	private boolean alreadyOpened;

	public DebugPanel(CommunicationHub hub) {
		super(hub);
		frame = new JFrame("LZRosterUpdater : Debug") {
			/**
			 * 
			 */
			private static final long serialVersionUID = -5953336285595669365L;

			@Override
			public void setVisible(boolean flag) {
				prepareWindow(this);
				super.setVisible(flag);
			}
			
		};
	}

	
	
	public JFrame getGui() {
		return frame;
	}

	@Override
	public void initializeCommunication() {
		logger = hub.getComponent(LZULogger.class);
		mainGui = hub.getComponent(MainGui.class);
	}

	@Override
	public void initialize() {
		initializeGui();
		Dictionary.addLocaleListener(this);

		
		frame.setContentPane(jTab);
		frame.pack();
	}

	private void prepareWindow(JFrame frame) {
		if (!alreadyOpened) {
			frame.setSize(mainGui.getGui().getSize());
			int x = mainGui.getGui().getX();
			int y = mainGui.getGui().getY();
			
			frame.setLocation(x+20, y+20);
			alreadyOpened = true;
		}
	}
	
	private void initializeGui() {
		jTab = new JTabbedPane();

		// Debug logger panel
		AbstractLoggerPanel debugLog = new AbstractLoggerPanel() {
			@Override
			public LoggerLevel getLoggerLevel() {
				return LoggerLevel.DEBUG;
			}

			@Override
			public void clear() {
				jTab.removeAll();
				jTab.add(this.getGui());
				updateTitles();
			}
		};

		jTab.add(debugLog.getGui());
		
		updateTitles();

		logger.addLoggerListener(debugLog, true);
		
	}
	
	private void updateTitles() {
		if (jTab != null && jTab.getTabCount() > 0) {
			jTab.setTitleAt(0, debugTitle.toString());
			for (int i = 1; i < jTab.getTabCount(); i++) {
				jTab.setTitleAt(i, pageTitle.useAsFormater(i));
			}
		}
	}
	
	public void addHtmlPage(HTTPData data) {
		if (data.getSource() == null) return;
		JEditorPane pane = new JEditorPane();
		pane.setContentType("text/html");

		JScrollPane jScroll = new JScrollPane(pane,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		StringBuffer buffer = new StringBuffer();
		boolean wait = true;
		for (String s : data.getSource()) {
			if (s.contains("</body>")) wait = true;
			if (!wait) buffer.append(s).append("\n");
			if (s.contains("<body>")) wait = false;
		}
		
		HTMLEditorKit kit = (HTMLEditorKit) pane.getEditorKit();
		javax.swing.text.Document doc = pane.getDocument();
		
		try {
			kit.read(new StringReader(buffer.toString()), doc, 0);
		} catch (Exception e) {
			//
		}
		int idx = jTab.getTabCount();
		jTab.add(pageTitle.useAsFormater(idx), jScroll);
		pane.setEditable(false);
	}

	public String getKey() {
		return null;
	}

	public void localeChange(Locale locale) {
		updateTitles();
	}
	
	
}
